| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332 |
1
30
30
34
34
30
30
30
34
28
34
28
34
34
34
34
16
34
34
34
30
34
34
32
32
34
30
1
30
35
1
1
1
1
1
30
32
30
105
2
| /**
* @ngdoc directive
* @name patternfly.charts.component:pfSparklineChart
* @restrict E
*
* @description
* Component for rendering a sparkline chart.
* <br><br>
* See http://c3js.org/reference.html for a full list of C3 chart options.
*
* @param {object} config configuration settings for the sparkline chart:<br/>
* <ul style='list-style-type: none'>
* <li>.chartId - the ID of the container that the chart should bind to
* <li>.units - unit label for values, ex: 'MHz','GB', etc..
* <li>.tooltipType - (optional) set the type of tooltip, valid values:
* <ul style='list-style-type: none'>
* <li>'default' - show the data point value and the data point name.
* <li>'usagePerDay' - show the date, percent used, and used value for the data point.
* <li>'valuePerDay' - show the date and value for the data point.
* <li>'percentage' - show the current data point as a percentage.
* </ul>
* <li>.tooltipFn - (optional) override the tooltip contents generation functions. Should take a data point and
* return HTML markup for the tooltip contents. Setting this overrides the tooltipType value.
* <li>.area - (optional) overrides the default Area properties of the C3 chart
* <li>.size - (optional) overrides the default Size properties of the C3 chart
* <li>.axis - (optional) overrides the default Axis properties of the C3 chart
* <li>.color - (optional) overrides the default Color properties of the C3 chart
* <li>.legend - (optional) overrides the default Legend properties of the C3 chart
* <li>.point - (optional) overrides the default Point properties of the C3 chart
* </ul>
*
* @param {object} chartData the data to be shown as an area chart<br/>
* <ul style='list-style-type: none'>
* <li>.xData - Array, X values for the data points, first element must be the name of the data
* <li>.yData - Array, Y Values for the data points, first element must be the name of the data
* <li>.total - (optional) The Total amount, used when determining percentages
* <li>.dataAvailable - Flag if there is data available - default: true
* </ul>
*
* @param {int=} chartHeight height of the sparkline chart
* @param {boolean=} showXAxis override config settings for showing the X Axis
* @param {boolean=} showYAxis override config settings for showing the Y Axis
* @example
<example module="patternfly.charts">
<file name="index.html">
<div ng-controller="ChartCtrl" class="row" style="display:inline-block; width: 100%;">
<div class="col-md-12">
<pf-sparkline-chart config="config" chart-data="data" chart-height="custChartHeight" show-x-axis="custShowXAxis" show-y-axis="custShowYAxis"></pf-sparkline-chart>
</div>
<hr class="col-md-12">
<div class="col-md-12">
<form role="form">
<div class="form-group">
<label>Tooltip Type</label>
</br>
<label class="radio-inline">
<input type="radio" ng-model="config.tooltipType" value="default">Default</input>
</label>
<label class="radio-inline">
<input type="radio" ng-model="config.tooltipType" value="usagePerDay">Usage Per Day</input>
</label>
<label class="radio-inline">
<input type="radio" ng-model="config.tooltipType" value="valuePerDay">Value Per Day</input>
</label>
<label class="radio-inline">
<input type="radio" ng-model="config.tooltipType" value="percentage">Percentage</input>
</label>
</div>
</form>
<div class="row">
<div class="col-md-6">
<form role="form"">
<div class="form-group">
<label>Show</label>
</br>
<label class="checkbox-inline">
<input type="checkbox" ng-model="custShowXAxis">X Axis</input>
</label>
<label class="checkbox-inline">
<input type="checkbox" ng-model="custShowYAxis">Y Axis</input>
</label>
</div>
</form>
</div>
<div class="col-md-3">
<form role="form" >
<div class="form-group">
<label>Chart Height</label>
</br>
<input style="height:25px; width:60px;" type="number" ng-model="custChartHeight"></input>
</div>
</form>
</div>
<div class="col-md-3">
<button ng-click="addDataPoint()">Add Data Point</button>
</div>
</div>
<div class="row">
<div class="col-md-6">
<form role="form"">
<div class="form-group">
<label class="checkbox-inline">
<input type="checkbox" ng-model="data.dataAvailable">Data Available</input>
</label>
</div>
</form>
</div>
</div>
</div>
</div>
</file>
<file name="script.js">
angular.module( 'patternfly.charts' ).controller( 'ChartCtrl', function( $scope ) {
$scope.config = {
chartId: 'exampleSparkline',
tooltipType: 'default'
};
var today = new Date();
var dates = ['dates'];
for (var d = 20 - 1; d >= 0; d--) {
dates.push(new Date(today.getTime() - (d * 24 * 60 * 60 * 1000)));
}
$scope.data = {
dataAvailable: true,
total: 100,
xData: dates,
yData: ['used', 10, 20, 30, 20, 30, 10, 14, 20, 25, 68, 54, 56, 78, 56, 67, 88, 76, 65, 87, 76]
};
$scope.custShowXAxis = false;
$scope.custShowYAxis = false;
$scope.custChartHeight = 60;
$scope.addDataPoint = function () {
$scope.data.xData.push(new Date($scope.data.xData[$scope.data.xData.length - 1].getTime() + (24 * 60 * 60 * 1000)));
$scope.data.yData.push(Math.round(Math.random() * 100));
};
});
</file>
</example>
*/
angular.module('patternfly.charts').component('pfSparklineChart', {
bindings: {
config: '<',
chartData: '<',
chartHeight: '<?',
showXAxis: '<?',
showYAxis: '<?'
},
templateUrl: 'charts/sparkline/sparkline-chart.html',
controller: function (pfUtils) {
'use strict';
var ctrl = this, prevChartData;
ctrl.updateAll = function () {
// Need to deep watch changes in chart data
prevChartData = angular.copy(ctrl.chartData);
// Create an ID for the chart based on the chartId in the config if given
if (ctrl.sparklineChartId === undefined) {
ctrl.sparklineChartId = 'sparklineChart';
Eif (ctrl.config.chartId) {
ctrl.sparklineChartId = ctrl.config.chartId + ctrl.sparklineChartId;
}
}
/*
* Setup Axis options. Default is to not show either axis. This can be overridden in two ways:
* 1) in the config, setting showAxis to true will show both axes
* 2) in the attributes showXAxis and showYAxis will override the config if set
*
* By default only line and the tick marks are shown, no labels. This is a sparkline and should be used
* only to show a brief idea of trending. This can be overridden by setting the config.axis options per C3
*/
if (ctrl.showXAxis === undefined) {
ctrl.showXAxis = (ctrl.config.showAxis !== undefined) && ctrl.config.showAxis;
}
if (ctrl.showYAxis === undefined) {
ctrl.showYAxis = (ctrl.config.showAxis !== undefined) && ctrl.config.showAxis;
}
ctrl.defaultConfig = patternfly.c3ChartDefaults().getDefaultSparklineConfig();
ctrl.defaultConfig.axis = {
x: {
show: ctrl.showXAxis === true,
type: 'timeseries',
tick: {
format: function () {
return '';
}
}
},
y: {
show: ctrl.showYAxis === true,
tick: {
format: function () {
return '';
}
}
}
};
// Setup the default configuration
ctrl.defaultConfig.tooltip = ctrl.sparklineTooltip();
if (ctrl.chartHeight) {
ctrl.defaultConfig.size.height = ctrl.chartHeight;
}
ctrl.defaultConfig.units = '';
// Convert the given data to C3 chart format
ctrl.config.data = pfUtils.merge(ctrl.config.data, ctrl.getSparklineData(ctrl.chartData));
// Override defaults with callers specifications
ctrl.chartConfig = pfUtils.merge(ctrl.defaultConfig, ctrl.config);
};
/*
* Convert the config data to C3 Data
*/
ctrl.getSparklineData = function (chartData) {
var sparklineData = {
type: 'area'
};
if (chartData && chartData.dataAvailable !== false && chartData.xData && chartData.yData) {
sparklineData.x = chartData.xData[0];
sparklineData.columns = [
chartData.xData,
chartData.yData
];
}
return sparklineData;
};
ctrl.getTooltipTableHTML = function (tipRows) {
return '<div class="module-triangle-bottom">' +
' <table class="c3-tooltip">' +
' <tbody>' +
tipRows +
' </tbody>' +
' </table>' +
'</div>';
};
ctrl.sparklineTooltip = function () {
return {
contents: function (d) {
var tipRows;
var percentUsed = 0;
Eif (ctrl.config.tooltipFn) {
tipRows = ctrl.config.tooltipFn(d);
} else {
switch (ctrl.config.tooltipType) {
case 'usagePerDay':
if (ctrl.chartData.dataAvailable !== false && ctrl.chartData.total > 0) {
percentUsed = Math.round(d[0].value / ctrl.chartData.total * 100.0);
}
tipRows =
'<tr>' +
' <th colspan="2">' + d[0].x.toLocaleDateString() + '</th>' +
'</tr>' +
'<tr>' +
' <td class="name">' + percentUsed + '%:' + '</td>' +
' <td class="value text-nowrap">' + d[0].value + ' ' + (ctrl.config.units ? ctrl.config.units + ' ' : '') + d[0].name + '</td>' +
'</tr>';
break;
case 'valuePerDay':
tipRows =
'<tr>' +
' <td class="value">' + d[0].x.toLocaleDateString() + '</td>' +
' <td class="value text-nowrap">' + d[0].value + ' ' + d[0].name + '</td>' +
'</tr>';
break;
case 'percentage':
percentUsed = Math.round(d[0].value / ctrl.chartData.total * 100.0);
tipRows =
'<tr>' +
' <td class="name">' + percentUsed + '%' + '</td>' +
'</tr>';
break;
default:
tipRows = patternfly.c3ChartDefaults().getDefaultSparklineTooltip().contents(d);
}
}
return ctrl.getTooltipTableHTML(tipRows);
},
position: function (data, width, height, element) {
var center;
var top;
var chartBox;
var graphOffsetX;
var x;
try {
center = parseInt(element.getAttribute('x'));
top = parseInt(element.getAttribute('y'));
chartBox = document.querySelector('#' + ctrl.sparklineChartId).getBoundingClientRect();
graphOffsetX = document.querySelector('#' + ctrl.sparklineChartId + ' g.c3-axis-y').getBoundingClientRect().right;
x = Math.max(0, center + graphOffsetX - chartBox.left - Math.floor(width / 2));
return {
top: top - height,
left: Math.min(x, chartBox.width - width)
};
} catch (e) {
}
}
};
};
ctrl.$onChanges = function (changesObj) {
ctrl.updateAll();
};
ctrl.$doCheck = function () {
// do a deep compare on chartData
if (!angular.equals(ctrl.chartData, prevChartData)) {
ctrl.updateAll();
}
};
}
});
|